﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Utility
{
    public class EmailHelper
    {
        public class FileAttachment
        {
            public String FileName { get; set; }
            public byte[] FileContent { get; set; }
        }
        private static readonly string emailaddress = ConfigurationManager.AppSettings["EmailAddress"];
        private static readonly string emailname = ConfigurationManager.AppSettings["EmailName"];
        private static readonly string emailip = ConfigurationManager.AppSettings["EmailIp"];
        private static readonly string emailport = ConfigurationManager.AppSettings["EmailPort"];
        private static readonly string emailerror = ConfigurationManager.AppSettings["EmailError"];

        public static bool SendEmail(string smtpServer, string smtpUser, string smtpPwd, bool enableSsl, string nickName, string toMail, string subj, string bodys, List<string> attachmentFileName = null)
        {
            var result = false;

            SmtpClient smtpClient = new SmtpClient();

            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式

            smtpClient.Host = smtpServer;//指定SMTP服务器

            smtpClient.Credentials = new NetworkCredential(smtpUser, smtpPwd);//用户名和密码

            smtpClient.EnableSsl = enableSsl;

            MailAddress fromAddress = new MailAddress(smtpUser, nickName);

            MailAddress toAddress = new MailAddress(toMail);

            MailMessage mailMessage = new MailMessage(fromAddress, toAddress);

            if (attachmentFileName != null && attachmentFileName.Count > 0)
            {
                foreach (var att in attachmentFileName)
                {
                    Attachment objFile = new System.Net.Mail.Attachment(att);
                    objFile.Name = Path.GetFileName(att);
                    mailMessage.Attachments.Add(objFile);
                }

            }

            mailMessage.Subject = subj;//主题

            mailMessage.Body = bodys;//内容

            mailMessage.BodyEncoding = Encoding.Default;//正文编码

            mailMessage.IsBodyHtml = true;//设置为HTML格式

            mailMessage.Priority = MailPriority.Normal;//优先级

            smtpClient.Send(mailMessage);
            result = true;

            return result;
        }

        public static bool SendEmail(string smtpServer, string smtpUser, string smtpPwd, bool enableSsl, string nickName, List<string> toMails, string subj, string bodys, List<FileAttachment> attachmentFileName = null)
        {
            var result = false;

            SmtpClient smtpClient = new SmtpClient();

            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式

            smtpClient.Host = smtpServer;//指定SMTP服务器

            smtpClient.Credentials = new NetworkCredential(smtpUser, smtpPwd);//用户名和密码

            smtpClient.EnableSsl = enableSsl;

            MailAddress fromAddress = new MailAddress(smtpUser, nickName);

            MailMessage mailMessage = new MailMessage();

            mailMessage.From = fromAddress;

            toMails.ForEach(p =>
            {
                MailAddress mailAddress = new MailAddress(p);
                mailMessage.To.Add(mailAddress);
            });

            if (attachmentFileName != null && attachmentFileName.Count > 0)
            {
                foreach (var fileAttachment in attachmentFileName)
                {
                    mailMessage.Attachments.Add(new Attachment(new MemoryStream(fileAttachment.FileContent),
                        fileAttachment.FileName));
                }

                //foreach (var att in attachmentFileName)
                //{
                //    Attachment objFile = new System.Net.Mail.Attachment(att);
                //    objFile.Name = Path.GetFileName(att);
                //    mailMessage.Attachments.Add(objFile);
                //}

            }

            mailMessage.Subject = subj;//主题

            mailMessage.Body = bodys;//内容

            mailMessage.BodyEncoding = Encoding.Default;//正文编码

            mailMessage.IsBodyHtml = true;//设置为HTML格式

            mailMessage.Priority = MailPriority.Normal;//优先级

            smtpClient.Send(mailMessage);
            result = true;

            return result;
        }

        /// <summesgary>
        /// 邮件发送
        /// </summesgary>
        /// <param name="mailto">收件人</param>
        /// <param name="title">主题</param>
        /// <param name="body">类容</param>
        /// <param name="attachment">附件地址</param>
        /// <param name="valid">是否发送</param>
        public static void SendToEmail(List<string> mailto, string title, string body, bool valid = true, List<string> attachment = null)
        {
            if (valid)
            {
                SmtpClient smtp = new SmtpClient();
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.EnableSsl = false;
                smtp.Host = emailip;
                smtp.Port = Convert.ToInt32(emailport);
                smtp.UseDefaultCredentials = true;
                //认证
                //smtp.Credentials = new NetworkCredential(this.FromEmailAddress, this.FormEmailPassword);    
                MailMessage mmesg = new MailMessage();
                //优先级 Low, Normal, High
                mmesg.Priority = MailPriority.Normal;
                mmesg.From = new MailAddress(emailaddress, emailname, Encoding.GetEncoding(936));
                //To发送人; CC抄送人; Bcc密抄人
                foreach (var item in mailto)
                {
                    mmesg.To.Add(new MailAddress(item));
                }
                //mmesg.Bcc.Add(new MailAddress(emailerror));
                mmesg.Subject = title;
                mmesg.SubjectEncoding = Encoding.GetEncoding(936);
                mmesg.IsBodyHtml = true;
                mmesg.BodyEncoding = Encoding.GetEncoding(936);
                mmesg.Body = body;
                if (attachment != null && attachment.Count > 0)
                {
                    foreach (var att in attachment)
                    {
                        Attachment objFile = new System.Net.Mail.Attachment(att);
                        objFile.Name = Path.GetFileName(att);
                        mmesg.Attachments.Add(objFile);
                    }
                }
                smtp.Send(mmesg);
            }
        }
    }
}
